var lang;
var recommendNum;
var userName;
$(document)
		.ready(
				function() {
					lang = $("#display-lang").val();
					recommendNum = 5;
					userName = $("#user").val();
					init();
					
					/* input hints */
					$("#query").autocomplete({
						source : function(request, response) {
							var data = new Object();
							data["kw"] = request.term;
							data["lang"] = lang;
							data = JSON.stringify(data);
							data = ("hint=" + encodeURIComponent(data));
							$.ajax({
								url : "/sview/dataset",
								dataType : "json",
								data : data,
								type : "post",
								success : function(data) {
									response(data.result);
								}
							});
						},
						minLength : 1,
						select : function(event, ui) {
						}
					}).data("autocomplete")._renderItem = function(ul, item) {
						var li = $("<li class='hitem'></li>");// class='hitem lentity'
						var div = $('<div class="split"></div>');
						var a = $('<a></a>');
						var href = "/sview/dataset/";
						var genre = item.genre;
						var id = item.id;
						var spanName = $("<span></span>");// class='entity'
						$(spanName).addClass("dataset");
						if (genre == "custom"){
							href = href + "c";
							$(li).addClass("ldataset");//.addClass("lcollection");
							$(spanName).addClass("custom");//.addClass("collection");
						}else if (genre == "uri"){
							href = href + "u";
							$(li).addClass("ldataset");//$(li).addClass("lentity");
							$(spanName).addClass("uri");//.addClass("entity");
						}
						href = href + id;
						if(lang=="zh"){
							href += "?lang=zh";
						}
						a.attr("href", href);
						div.append(a);
						spanName.text(item.name);
						a.append(spanName);
						li.append(div);
						li.appendTo(ul);
						ul.find("li:last>div").addClass("final");
						return li;
					};

					$("#searchq").click(function(){
				    	$(".container").hide();
				    	var loadDiv = $('<div class="load-data2"></div>');
				    	$(".container").after(loadDiv);
				    	$(this).parent().parent().submit();
				    });
					
					$(".hitem a").live("click", function() {
						window.location = $(this).attr("href");
					});

					   /*navigation tabs*/
					$(".tab a").click(function(){
						 if(!$(this).parent().hasClass("selected")){
							   $(".tab-header .tab.selected").each(function(){
								   $(this).removeClass("selected");
							   });
							   $(this).parent().addClass("selected");
						   }
						 
						   if($(this).hasClass("pe")){
								 //process the popular entities
							   showLists("dataset","popAll", 0, recommendNum, lang, true);
							   }else if($(this).hasClass("me")){
								 //process mine entities
								   showLists("bookmarkShow","mineBookmark", 0, recommendNum, lang, true, userName);
						   }
					});
					
					$(".leftnav a").click(function() {
						if (!$(this).hasClass("nav-select")) {
							$(".leftnav a.nav-select").each(function() {
										$(this).removeClass("nav-select");
										$(this).parent().find(".nav-arrow").remove();
									});
							$(this).addClass("nav-select");
							var span = $("<span class='nav-arrow'></span>");
							$(this).parent().append(span);

						}

						$(".op select option.all").attr("selected", true);

						if ($(this).hasClass("pe")) {
							// process the popular entities
							showLists("dataset","popAll", 0, recommendNum, lang, true);
						} else if ($(this).hasClass("me")) {
							// process mine entities
//							showDatasets("mineAll", 0, 20,
//									lang, true, userName);
							showLists("bookmarkShow","mineBookmark", 0, recommendNum, lang, true, userName);
						} /*else if ($(this).hasClass("create")){
							alert("create");
//							window.location.href="relations/dataset_create.jsp?lang="+lang+"&genre=";
						}*/
					});//end of click
			
					/*$("form")
							.submit(
									function(e) {
										e.preventDefault();
										var query = $("#query").val();

										var data = {};
										data["kw"] = encodeURIComponent(query);
										data["lang"] = lang;
										data = JSON.stringify(data);
										data = ("query=" + encodeURIComponent(data));
										$
												.ajax({
													url : "/sview/dataset",
													dataType : "json",
													data : data,
													type : "post",
													success : function(result) {
														if (result["result"] == "") {
															var infoText;
															if(lang == 'en'){
																infoText = "No such dataset.";
															}else if(lang == 'zh'){
																infoText = "该数据集不存在。";
															}
															$(".container")
																	.html(
																			"<div style='position:absolute; left:40%; top:30%; color: red; font-size:200%'>"+infoText+"</div>");
														} else {
															var href = "/sview/dataset/";
															result = result["result"];
															var genre = result["genre"];
															var id = result["id"];
															if (genre == "custom")
																href = href
																		+ "c";
															else if (genre == "uri")
																href = href
																		+ "u";
															href = href + id;
															window.location = href;
														}
													}
												});
									});*/

					$(".entity-list li,.hitem").live("mouseover", function() {
						$(this).addClass("bh");
					});

					$(".entity-list li,.hitem").live("mouseleave", function() {
						$(this).removeClass("bh");
					});
				});

function init() {
	if($(".me").length>0){
		$(".me").parent().addClass("selected");
		showLists("bookmarkShow","mineBookmark", 0, recommendNum, lang, true, userName);
    }else  {
	   $(".pe").parent().addClass("selected");
	   showLists("dataset","popAll", 0, recommendNum, lang, true);
    }
}

function showLists(url, optype, start, length, lang, newFlag, user) {
	$(".entity-list").addClass("loading2");
	var data = {};
	data["type"] = optype;
	data["start"] = start;
	data["length"] = length;
	data["lang"] = lang;
	data["user"] = user;
	data = JSON.stringify(data);
	data = ("info=" + encodeURIComponent(data));
	$.ajax({
				url : "/sview/"+url,//"dataset",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					$(".entity-list").removeClass("loading2");
					var lang = $("#display-lang").val();
					var array = result["result"];
					var flag = false;
					if (newFlag) {
						$(".entity-list").html("");
					}
					if ($(".entity-list ol").length > 0)
						flag = true;
					if (!flag) {
						var olt = $("<ol></ol>");
						$(".entity-list").append(olt);
					}
					var ol = $(".entity-list>ol");
					for ( var i = 0; i < array.length; i++) {
						var li = $("<li></li>");
						ol.append(li);
						var element = array[i];
						var name = element["name"];
						if(name==null){
							alert(name+"|"+element["id"]+"|"+element);
							continue;
						}
						var genre = element["genre"];
						li.addClass("ldataset");
//						if (genre == "custom") {
////							li.addClass("lcollection");
//							li.addClass("custom");
//						} else if(genre == "uri"){
////							li.addClass("lentity");
//							li.addClass("uri");
//						}
						var id = element["id"];
						var div = $('<div class="sc split"></div>');
						li.append(div);
						var a = $("<a></a>");
						var href = "/sview/dataset/";
						if (genre == "custom")
							href = href + "c";
						else if (genre == "uri")
							href = href + "u";
						href = href + id;
						if(lang == "zh"){
							href += "?lang=zh";
						}
						a.attr("href", href);
						div.append(a);
						var spanName = $("<span></span>");
						var spanType = $("<span class='type'></span>");
						spanName.text(name);
						var uri = element["uri"];
						if(uri != null){
							spanName.attr("title",uri);
						}
						spanName.addClass("dataset");
						if (genre == "custom") {
//							spanName.addClass("collection");
							spanName.addClass("custom");
						} else{
//							spanName.addClass("entity");
							spanName.addClass("uri");
						}
						a.append(spanName);
						a.append(spanType);
					}
					$(".entity-list ol li:last>div").addClass("final");
					$(".more-container").remove();
					if (array.length > 0) {
						var div = $("<div class='more-container'></div>");
						var moreDiv = $("<div class='moreDiv'></div>");
						var a = null;
						if (lang == 'en')
							a = $('<a href="javascript:void(0)"><span class="tip">Load More</span><span class="arrowDown"></span></a>');
						else if (lang == 'zh')
							a = $('<a href="javascript:void(0)"><span class="tip">更多</span><span class="arrowDown"></span></a>');
						a.bind("click", function() {
							showLists(url,optype, start + length, length, lang,
									false, user);
						});
						moreDiv.append(a);
						div.append(moreDiv);
						$(".entity-list").parent().append(div);
					}
				}
			});
}
